package it.asvp.asvp_portal.web.controller;

import it.asvp.asvp_portal.asvp_util.util.Constants;
import it.asvp.asvp_portal.service.services.EventService;
import it.asvp.asvp_portal.service.services.NewsService;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/home")
public class HomeController extends BaseController{

	private static final Log log = LogFactory.getLog(HomeController.class);
	
	@Autowired
	EventService eventService;
	@Autowired
	NewsService newsService;
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView home() {

		log.info("Accesso alla homepage");
		try{
			
			ModelAndView mv = getModelAndView("home");
			mv.addObject(Constants.Param.TITLE, Constants.Home.TITLE);
		    mv.addObject(Constants.Event.PRIORITY_EVENTS, eventService.getByPriority(true));
		    mv.addObject(Constants.News.NEWS, newsService.getAllNews());
		    return mv;	
		    
		} catch (Exception e) {
			return getErrorModelAndView(e);
		}
	}
	
	@RequestMapping(value = "/addLoginMessage",method = RequestMethod.GET)
	public ModelAndView addLoginMessage(@RequestParam(value="message") String message) {

		log.info("Accesso alla homepage con messaggio");
		
		try{
			
			ModelAndView mv = getModelAndView("home");
			mv.addObject("message", message);
		    mv.addObject(Constants.Event.PRIORITY_EVENTS, eventService.getByPriority(true));
		    mv.addObject(Constants.News.NEWS, newsService.getAllNews());
		    return mv;	
		    
		} catch (Exception e) {
			return getErrorModelAndView(e);
		}
	}
	
}
